package com.self.cgqcompose.ui.components

import android.view.MotionEvent
import android.view.ViewGroup
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.foundation.border
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.positionChange
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import kotlin.math.abs

@Composable
fun WebViewPage(url: String, modifier: Modifier = Modifier) {
    AndroidView(
        modifier = modifier.fillMaxSize()
            //.border(2.dp, Color.Red)
            .clipToBounds(),
        factory = { context ->
            object: WebView(context){
                private var startX = 0f
                private var startY = 0f

                override fun onTouchEvent(event: MotionEvent): Boolean {
                    when (event.action) {
                        MotionEvent.ACTION_DOWN -> {
                            startX = event.x
                            startY = event.y
                            // 允许 WebView 优先处理触摸
                            parent.requestDisallowInterceptTouchEvent(true)
                        }
                        MotionEvent.ACTION_MOVE -> {
                            val deltaX = abs(event.x - startX)
                            val deltaY = abs(event.y - startY)
                            // 如果垂直拖动大于水平拖动，阻止父容器拦截
                            if (deltaY > deltaX) {
                                parent.requestDisallowInterceptTouchEvent(true)
                            } else {
                                // 水平拖动较多，允许 HorizontalPager 处理
                                parent.requestDisallowInterceptTouchEvent(false)
                            }
                        }
                        MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                            parent.requestDisallowInterceptTouchEvent(false)
                        }
                    }
                    return super.onTouchEvent(event)
                }
            }.apply {
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
                // 配置 WebView
                settings.javaScriptEnabled = true // 启用 JavaScript（根据需求设置）
                webViewClient = WebViewClient() // 确保网页在 WebView 内打开，而不是跳转到浏览器
                settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
                loadUrl(url) // 加载指定 URL
            }
        },
        update = { webView ->
            // 更新 WebView（例如 URL 变化时）
            webView.loadUrl(url)
        }
    )
}